package com.xbh.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nimbusds.jose.JOSEException;
import com.xbh.common.web.core.Result;
import com.xbh.management.entity.ManRoleVO;
import com.xbh.management.exception.JwtSignatureVerifyException;
import com.xbh.management.model.ManMenu;
import com.xbh.management.model.ManRole;
import com.xbh.management.service.RoleService;
import com.xbh.management.utils.JwtUtilManagement;
import com.xbh.management.utils.TreeNodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-09-25 14:28
 */


@RestController
@RequestMapping(path = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     * @param params
     * @return
     */
    @PostMapping("/page")
    public Result queryAllRole(@RequestBody JSONObject params){
        Long pageNum = params.getLong("pageNum");
        Long pageSize = params.getLong("pageSize");

        ManRole manRole = params.toJavaObject(ManRole.class);

        IPage<ManRole> manRoleIPage = roleService.queryAllRole(new Page<ManRole>(pageNum, pageSize), manRole);

        return Result.success(manRoleIPage);
    }

    @PostMapping("/list")
    public Result list(@RequestBody JSONObject params){
        LambdaQueryWrapper<ManRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManRole::getDeleted,new Integer(0));
        List<ManRole> list = roleService.list(wrapper);

        return Result.success(list);
    }
    /**
     * 添加角色
     * @param params
     * @return
     */
    @PostMapping("/save")
    public Result saveRole(@RequestBody JSONObject params) throws ParseException, JOSEException, JwtSignatureVerifyException {
        ManRole manRole = params.toJavaObject(ManRole.class);
        manRole.setCreatePerson(JwtUtilManagement.getUsername());
        manRole.setUpdatePerson(JwtUtilManagement.getUsername());
        int isFlag = roleService.saveRole(manRole);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("保存失败");
    }

    /**
     * 根据id查询角色
     * @param params
     * @return
     */
    @PostMapping("/selectRoleByRoleId")
    public Result queryRoleWithRoleId(@RequestBody JSONObject params){
        Long roleId = params.getLong("roleId");
        ManRole manRole = roleService.queryRoleWithRoleId(roleId);
        return Result.success(manRole);

    }

    /**
     * 根据角色id修改角色
     * @param params
     * @return
     */
    @PostMapping("/updateRoleByRoleId")
    public Result updateRoleWithRoleId(@RequestBody JSONObject params){
        ManRole manRole = params.toJavaObject(ManRole.class);
        int isFlag = roleService.updateRoleWithRoleId(manRole);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("修改失败");
    }

    /**
     * 查询当前用户所拥有的的角色
     * @param params
     * @return
     */
    @PostMapping("/selectRoleRelationAllUser")
    public Result queryUserRelationAllRole(@RequestBody JSONObject params){
        ManRole role = params.toJavaObject(ManRole.class);
        ManRoleVO manRoleVO = roleService.queryRoleRelationAllUser(role);
        if(manRoleVO==null){
            return Result.failed("004","没有数据");
        }
        return Result.success(manRoleVO);
    }

    /**
     * 查询当前角色所关联的菜单
     * @param params
     * @return
     */
    @PostMapping("/selectMenuByRole")
    public Result queryMenuByRole(@RequestBody JSONObject params){
        ManRole manRole = params.toJavaObject(ManRole.class);
        ManRoleVO manRoleVO = roleService.queryMenuBuRole(manRole);
        if(manRoleVO == null){
            return Result.failedParams("当前角色还没有关联菜单");
        }
        List<ManMenu> menuList = manRoleVO.getMenuList();
        List<ManMenu> manMenus = TreeNodeUtil.transformData(menuList, "119853844214895");
        if(manRoleVO==null){
            return Result.failed("004","没有数据");
        }
        return Result.success(manMenus);
    }
}
